package com.jlastu.dlxy_androidapp.view.details;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;

public class Service extends AppCompatActivity {
    Button startChatBtn;
    LinearLayout chatList;
    EditText mResultText;
    ImageView backButton ;
    ImageView user;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        initView();
        initButtonClickListener();
        //返回按钮
        backButton= findViewById(R.id.back_button);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Service.this, CommodityDetails.class);
                startActivity(intent);
                finish();
            }
        });
        //店铺用户按钮
        user=findViewById(R.id.user);
        user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Service.this, ShopUser.class);
                startActivity(intent);
            }
        });
    }
    private void initView() {
        startChatBtn = findViewById(R.id.start);
        chatList = findViewById(R.id.chat_output_text);
        mResultText = findViewById(R.id.messageInput);
    }
    private void initButtonClickListener() {
        // 为发送消息按钮设置点击事件
        Button sendButton = findViewById(R.id.start);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 调用发送消息的方法
                sendMessage();
            }
        });
    }

    // 发送消息的方法
    private void sendMessage() {
        // 获取用户输入的消息
        String message = mResultText.getText().toString();

        // 使用自定义的消息布局文件创建新的消息视图
        LinearLayout messageItem = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.message_item, null);
        TextView messageTextView = messageItem.findViewById(R.id.message_text);
        ImageView avatarImageView = messageItem.findViewById(R.id.avatar);

        // 设置消息文本和头像
        messageTextView.setText(message);
        avatarImageView.setImageResource(R.drawable.avatar); // 设置默认头像

        // 将新的消息视图添加到聊天列表中
        chatList.addView(messageItem);

        // 清空输入框
        mResultText.setText("");
        // 滚动到聊天界面底部
        toend();
    }
    public void toend() {
        chatList.post(new Runnable() {
            @Override
            public void run() {
               // chatList.fullScroll(View.FOCUS_DOWN);
            }
        });
    }
}